package kr.yoon.practice.db.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class ConnectionFactory {
	private String driver;
	private String url;
	private String user;
	private String password;
	
	
	
	public ConnectionFactory() {
		loadProperites();
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			System.err.printf("%s",  "MySQL Driver not Found");
			System.exit(-1);
		}
	} 

	private static ConnectionFactory instance = new ConnectionFactory();
	
	public static Connection getConnection(){
		return instance.createConnection();
	}

	private Connection createConnection() {
		Connection con = null;
		try {
			con = DriverManager.getConnection(url, user, password);  //DriverManager 클래스 일련의 JDBC 드라이버를 관리..
							//getConnection 지정된 데이터 베이스에 접속..
			System.out.println("MySQL Connection Success!!");
		} catch (SQLException e) {
			System.err.printf("%s",  "Error : Unable Connect to MySQL");
		}
		return con;
	} // end of createConnection Method
	
	private void loadProperites(){
		Properties prop = DBProperties.loadProperties("dbcon.xml");
		driver = prop.getProperty("DRIVER");
		url = prop.getProperty("URL");
		user = prop.getProperty("USER");
		password = prop.getProperty("PASSWORD");
	}
}
